package com.fenghongzhang.day007.search;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenghongzhang.day007.MyRVAdapter;
import com.fenghongzhang.day007.R;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private ScrollView scrollView;
    private LinearLayout ll;
    private LinearLayout searchBar;
    private EditText searchEdit;
    private ImageView searchBtn;
    private MyRv rv;
    private TextView searchLog;
    private ImageView btn;

    List<Search> list;
    RvAdapter rvAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);


        scrollView = (ScrollView) findViewById(R.id.scrollView);
        ll = (LinearLayout) findViewById(R.id.ll);
        searchBar = (LinearLayout) findViewById(R.id.search_bar);
        searchEdit = (EditText) findViewById(R.id.search_edit);
        searchBtn = (ImageView) findViewById(R.id.search_btn);
        rv = (MyRv) findViewById(R.id.rv);
        searchLog = (TextView) findViewById(R.id.search_log);
        btn = (ImageView) findViewById(R.id.btn);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        rv.setLayoutManager(linearLayoutManager);

        list =new ArrayList<>();
//        list.add(new Search(1l,"啊哈哈"));
//        list.add(new Search(2l,"啊哈哈"));
//        list.add(new Search(3l,"啊哈哈"));

        //从数据拿到数据库
        SearchDao searchDao = DaoManager.getInstance().getSearchDao();
        List<Search> daoList = searchDao.queryBuilder().orderDesc(SearchDao.Properties.Id).limit(10).list();
        list.addAll(daoList);

        rvAdapter = new RvAdapter(R.layout.search_rv_layout, this.list);
        rv.setAdapter(rvAdapter);
        
        rvAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(SearchActivity.this, "大", Toast.LENGTH_SHORT).show();
                Search item = rvAdapter.getItem(position);
                //集合
                list.remove(item);

                //数据库删除一下
                DaoManager.getInstance().getSearchDao().delete(item);
                rvAdapter.notifyDataSetChanged();
            }
        });
    }

    //添加记录
    public void add(View view) {
        //插入到数据库
        String title = searchEdit.getText().toString();
        Search search = new Search();
        search.setTitle(title);
        DaoManager.getInstance().getSearchDao().insert(search);

        //刷新适配器在页面展示
//        list.add(search);
        list.clear();
        SearchDao searchDao = DaoManager.getInstance().getSearchDao();
        List<Search> daoList = searchDao.queryBuilder().orderDesc(SearchDao.Properties.Id).limit(10).list();
        list.addAll(daoList);
        rvAdapter.notifyDataSetChanged();

    }


    public void showAll(View view) {
        if(rv.isFull()){
            searchLog.setText("展示所有");
            rv.setFull(false);
            new AlertDialog.Builder(this).setTitle("是否删除").setMessage("是否删除所有")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            DaoManager.getInstance().getSearchDao().deleteAll();
                            list.clear();
                            rvAdapter.notifyDataSetChanged();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).show();
        }else{
            searchLog.setText("删除所有");
            rv.setFull(true);

        }
    }
}
